package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.shopping.converter.PanelDtoConverter;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.service.IShoppingRecommendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author 1784549954@qq.com
 * @since 2022/12/03 09:03
 */
@Service
public class IShoppingRecommendServiceImpl implements IShoppingRecommendService {



    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    PanelDtoConverter panelDtoConverter;


    /*
         显示首页商品信息，显示的是Panel信息，但是这里注意下，如何获取Panel信息呢？
         1. 先调用 PanelMapper selectPanelContentById，自己看下sql语句，获取 List<Panel>
         2. Panel对象中，少了 List<PanelContentItem> panelContentItems
         3. 因此，需要遍历List中的Panel，在调用PanelContentMapper 的 selectPanelContentAndProductWithPanelId方法
            获取List<PanelContentItem>，利用MapStruct将其转化为 List<PanelContentItemDto>
         4. 然后set 到Panel

     */
    @Override
    public PanelDto recommend() {
        Example example = new Example(Panel.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("sortOrder",6);
        List<Panel> panels = panelMapper.selectByExample(example);
        for (Panel panel : panels) {
            List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
            panel.setPanelContentItems(panelContentItems);
        }
        PanelDto panelDto = panelDtoConverter.panelToPanelDto(panels.get(0));
        panelDto.setLimitNum(null);
        return panelDto;
    }
}
